﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Spring.Context;
using Spring.Context.Support;
using YiLe.ApplicationFolders;
using YiLe.View.Login;
using YiLe;

namespace YiLe.UI
{
    public partial class YiLeSystemApp
    {
        private ConfigDataSet mConfigDS;//配置dataset
        private bool mReadSettingFromDS;//是否读取文件
        private const string ConfigFileName = "YiLeSystemApp.nws";//配置文件名
        public static ConfigDataSet ConfigDataSet
        {
            get
            {
                return (ConfigDataSet)YiLeSystemApplication.mConfigDS;
            }
        }
        public static ConfigDataSet.ApplicationSettingsRow AppSettings
        {
            get
            {
                return (ConfigDataSet.ApplicationSettingsRow)YiLeSystemApplication.mConfigDS.ApplicationSettings.Rows[0];
            }
        }
        public static bool ReadSettingsFromDS
        {
            get
            {
                return YiLeSystemApplication.mReadSettingFromDS;
            }
        }
        /// <summary>
        /// 保存App数据
        /// </summary>
        public static void SaveAppSettings()
        {
            string strDataFile = GetLayout(ConfigFileName);
            RootFolder.SaveViewSettings(ConfigDataSet.FolderViews);
            YiLeSystemApplication.mConfigDS.WriteXml(strDataFile);
        }

        private static string GetLayout(string layoutName)
        {
            return System.IO.Path.Combine(YiLeSystemApp.LayoutPath(true), layoutName);
        }

        private static string LayoutPath(bool create)
        {
            FileInfo fileInfo = new FileInfo(Application.ExecutablePath);
            string strLayoutDirName = Path.Combine(fileInfo.DirectoryName, "AppSettings");
            if (create)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(strLayoutDirName);
                if (! dirInfo.Exists)
                {
                    dirInfo.Create();
                }
            }
            return strLayoutDirName;
        }
        /// <summary>
        /// 获取DataProvider
        /// </summary>
        public static DataProvider ActiveViewDataProvider
        {
            get
            {
                /*
                 * TODO:NewMainForm.CurrentFolder
                 * if (NewMainForm.CurrentFolder != null)
                {
                    return NewMainForm.CurrentFolder.DataProvider;
                }
                else
                {
                    return null;
                }*/
                return null;
            }
        }

        public static DataProvider GetDataProvider(Type providerType)
        {
            if (ActiveViewDataProvider != null)
            {
                if (ActiveViewDataProvider.GetType() == providerType)
                {
                    return ActiveViewDataProvider;
                }
            }
            return (DataProvider)(Activator.CreateInstance(providerType));
        }
        //异常消息标题
        public static string MessageCaption = "YiLeSystem exception message";
        private RootFolder mRootFolder;
        public static RootFolder RootFolder
        {
            get { return mApplication.mRootFolder; }
        }
        /// <summary>
        /// main 
        /// </summary>
        private LaterMainForm mMainForm;
        public static LaterMainForm MainForm
        {
            get
            {
                if (mApplication == null)
                {
                    return null;
                }
                return mApplication.mMainForm;
            }
        }
        //------------------------------------

        //LOG日志
        private static log4net.ILog LOG = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        //ApplicationContext(是否拆分成各个Service)
        private Spring.Context.IApplicationContext mContext;

        //登录页面LoginForm
        private NewLoginForm mLoginForm;

        //主页面MainForm

        //主程序(单例模式)
        public static YiLeSystemApp mApplication = null;


        public static void Initialize()
        {
            mApplication = new YiLeSystemApp();

            //initialize spring application context
            mApplication.mContext = ContextRegistry.GetContext();

            //create and initialize login form
            mApplication.mLoginForm = new NewLoginForm(mApplication);

            mApplication.mRootFolder = new RootFolder();
            try
            {
                mApplication.mLoginForm.Show();
                mApplication.mLoginForm.Activate();
                Application.Run(mApplication.mLoginForm);

            }
            catch (Exception e)
            {
                LOG.Info("YiLeSystemApp.cs启动失败，原因：" + e.ToString());
                MessageBox.Show(e.ToString());
            }

        }

        public static YiLeSystemApp YiLeSystemApplication
        {
            get
            {
                return mApplication;
            }
        }

        public NewLoginForm LoginForm
        {
            get
            {
                if (mLoginForm == null)
                {
                    return null;
                }
                return mApplication.mLoginForm;
            }
        }

        public IApplicationContext Context
        {
            get
            {
                if (mContext == null)
                {
                    mContext = ContextRegistry.GetContext();
                }
                return mContext;
            }
        }

        internal object GetProductList()
        {
            throw new NotImplementedException();
        }
    }
}
